package com.xuecheng.web.exceptions;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.ResponseResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Keson
 * @version 1.0.0
 * @title GlobalExceptionHandler
 * @description 全局异常处理器
 * @create 2022/7/14 16:28
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * @description: 全局处理运行时异常，返回固定错误(9999,服务器内部错误，请稍后再试)
     * @param: RuntimeException
     * @return: com.xuecheng.commons.model.vo.ResponseResult
     * @author Keson
     * @date: 2022/7/14 16:30
     */
    @ExceptionHandler(value = RuntimeException.class)
    public ResponseResult exception(RuntimeException e) {
        e.printStackTrace();
        return ResponseResult.errorResult(ErrorCode.ERROR);
    }

    /**
     * @description: 全局自定义异常处理
     * @param: BusinessException
     * @return: com.xuecheng.commons.model.vo.ResponseResult
     * @author Keson
     * @date: 2022/7/14 16:35
     */
    @ExceptionHandler(value = BusinessException.class)
    public ResponseResult businessException(BusinessException e) {
        ErrorCode errorCode = e.getErrorCode();
        return ResponseResult.errorResult(errorCode);
    }

}
